package com.hankai.common;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * Created by hankai on 2017/6/4.
 */
public class TokenCache {
    private static Logger logger = LoggerFactory.getLogger(TokenCache.class);
    //lru算法

    private static LoadingCache<String,String> localCache = CacheBuilder.newBuilder()
            .initialCapacity(1000)//缓存的出事话容量
            .maximumSize(10000)//当缓存超出10000的时候自动调用LRU算法
            .expireAfterAccess(12, TimeUnit.HOURS)//有效期12小时
            .build(new CacheLoader<String, String>() {
                //默认数据加载实现，当取值调用key时，没有key默认调用这个方法。
                @Override
                public String load(String s) throws Exception {
                    return "null";
                }
            });

    public static void setKey(String key,String value){
        localCache.put(key,value);
    }
    public static String getKey(String key){
        String value = null;
        try {
            value = localCache.get(key);
            if(value.equals("null")){
                return null;
            }
            return value;
        }catch (Exception e){
            logger.error("localCache get value",e);
        }
        return null;
    }
}
